<template>
  <div class="main">
    <mine-title>算法参数详情</mine-title>
    <div class="content" v-loading="loading">
      <el-row :gutter="20">
        <el-col :span="12">
          <div>
            <el-form
              ref="form"
              :model="form"
              label-width="110px"
              :rules="rules"
            >
              <el-form-item label="算法名称：" prop="algorithmName">
                <el-input
                  v-model="form.algorithmName"
                  size="small"
                  clearable
                  disabled
                ></el-input>
              </el-form-item>
              <el-form-item label="算法类型：" prop="algorithmClass">
                <el-input
                  v-model="form.algorithmClass"
                  size="small"
                  clearable
                  disabled
                ></el-input>
              </el-form-item>
              <el-form-item label="算法类型编码：" prop="algorithmType">
                <el-input
                  v-model="form.algorithmType"
                  size="small"
                  clearable
                  disabled
                ></el-input>
              </el-form-item>
              <el-form-item label="算法参数：" prop="parameter">
                <el-input
                  v-model="form.parameter"
                  type="textarea"
                  size="small"
                  clearable
                  disabled
                  autosize
                ></el-input>
              </el-form-item>
              <el-form-item label="模板类型" prop="projectType">
                <el-input
                  v-model="form.projectType"
                  size="small"
                  clearable
                  disabled
                ></el-input>
              </el-form-item>
              <el-form-item label="版本" prop="version">
                <el-input
                  v-model="form.version"
                  size="small"
                  clearable
                  disabled
                ></el-input>
              </el-form-item>
              <!-- <el-form-item label="发布时间：" prop="canvasCount">
                                <el-input
                                    v-model="form.canvasCount"
                                    size="small"
                                    clearable
                                    disabled
                                ></el-input>
                            </el-form-item>
                            <el-form-item label="创建人：" prop="creator">
                                <el-input
                                    v-model="form.creator"
                                    size="small"
                                    clearable
                                    disabled
                                ></el-input>
                            </el-form-item> -->

              <el-form-item class="form-btn-row">
                <mine-button type="primary" @click="onCancel" secondary
                  >返 回</mine-button
                >
                <!-- <mine-button type="primary" @click="onSubmit"
                                    >保 存</mine-button
                                > -->
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="12"> </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import request from "@/modules/index/utils/request";
import _ from "lodash";
import { QWebChannel } from "@/modules/index/utils/qwebchannel.js";
import qs from "qs";
export default {
  name: "itemDetail",
  data() {
    return {
      form: {},
      rules: {},
      loading: false,
    };
  },
  computed: {},
  methods: {
    async init() {
      await this.getInstance();
    },
    getInstance: async function () {
      let id = this.$route.query.id;
      let params = id;
      try {
        this.loading = true;
        let res = {};
        let _self = this;
        if (_self.$qwebUtil.isQtClient) {
          const uuid = _self.$qwebUtil.generateRandomUUID();
          let sendObject;
          /* eslint-disable-next-line */
          const channel = new QWebChannel(
            window.qt.webChannelTransport,
            async function (channel) {
              sendObject = channel.objects.sendObject;
              // receivedJSON 修改此处发送请求参数
              const receivedJSON = {
                RequestUrl: `/algorithmInfo/getById?id=${params}`,
                RequestMethod: "get",
                ContentType: "application/json;charset=UTF-8",
                RequestBody: {},
                isStringParam: false,
                requestId: uuid,
              };
              await send();
              await _self.$qwebUtil.receive(sendObject, receivedJSON);
              // console.error('received','/feCanvas/pageList')
            }
          );
          /* eslint-disable-next-line */
          async function send() {
            sendObject.sendMessage.connect(function (data) {
              if (!data) {
                console.error("No data received.");
              }
              res = JSON.parse(data);
              const result = res.response || {};
              // update the followings
              if (result.success && res.webInfo.requestId == uuid) {
                const data = result.data || {};
                _self.form = data;
                _self.form.parameter = JSON.parse(_self.form.parameter);
                _self.form.parameter = JSON.stringify(
                  _self.form.parameter,
                  null,
                  4
                );
                _self.loading = false;
              }
            });
          }
        } else if (this.$webview2.isWPF) {
          const uuid = this.$webview2.generateRandomUUID();
          await this.$webview2.sendWebView2(
            `/algorithmInfo/getById?id=${params}`,
            "get",
            "application/json;charset=UTF-8",
            {},
            false,
            uuid
          );
          await window.chrome.webview.addEventListener("message", (e) => {
            let res = e.data;
            // console.log(res);
            const result = res.response || {};
            console.log(result);
            if (result.success && res.webInfo.requestId == uuid) {
              const data = result.data || {};
              this.form = data;
              this.form.parameter = JSON.parse(this.form.parameter);
              this.form.parameter = JSON.stringify(
                this.form.parameter,
                null,
                4
              );
              this.loading = false;
            }
            // console.log(result);
          });
        } else {
          const res = await this.$store.dispatch("algoParamsListById", params);
          const result = res.data || {};
          if (result.success) {
            const data = result.data || {};
            this.form = data;
            this.form.parameter = JSON.parse(this.form.parameter);
            this.form.parameter = JSON.stringify(this.form.parameter, null, 4);
            this.loading = false;
          }
        }
      } catch (e) {
        console.error(e);
        this.loading = false;
      }
    },
    onSubmit() {
      let _this = this;
      this.$refs.form.validate(async (valid, object) => {
        let { form, equipCode, equipName, equipUse } = _this;
        if (valid) {
          this.loading = true;

          request({
            url: "itemInfo/saveOrUpdate", //接口
            method: "post",
            data: form,
          }).then((response) => {
            let result = response.data;
            if (!result.success) {
              this.loading = false;
              return;
            }
            this.$confirm("保存成功，是否需要返回列表？", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                this.$router.push("/algoParams");
              })
              .catch(() => {
                this.loading = false;
              });
            // _this.onSubmitFile(result.data.id);
          });
        } else {
          return false;
        }
      });
    },
    onCancel() {
      this.$router.push("/algoParams");
      this.form = {};
    },
  },
  created() {
    this.init();
    this.$store.commit("SET_BREADCRUMB", [
      { title: "算法参数管理", path: "/algoParams" },
      { title: "算法参数详情", path: "" },
    ]);
  },
  mounted() {},
};
</script>

<style scoped></style>
